package com.ruoyi.teacher.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.teacher.domain.TBookAuthor;
import com.ruoyi.teacher.mapper.TBookMapper;
import com.ruoyi.teacher.domain.TBook;
import com.ruoyi.teacher.service.ITBookService;

/**
 * 著作信息集Service业务层处理
 *
 * @author xiangzy
 * @date 2022-05-10
 */
@Service
public class TBookServiceImpl implements ITBookService
{
    @Autowired
    private TBookMapper tBookMapper;

    /**
     * 查询著作信息集
     *
     * @param recordid 著作信息集ID
     * @return 著作信息集
     */
    @Override
    public TBook selectTBookById(Long recordid)
    {
        return tBookMapper.selectTBookById(recordid);
    }

    /**
     * 查询著作信息集列表
     *
     * @param tBook 著作信息集
     * @return 著作信息集
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<TBook> selectTBookList(TBook tBook)
    {
        return tBookMapper.selectTBookList(tBook);
    }

    /**
     * 新增著作信息集
     *
     * @param tBook 著作信息集
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTBook(TBook tBook)
    {
        tBook.setCreateTime(DateUtils.getNowDate());
        int rows = tBookMapper.insertTBook(tBook);
        insertTBookAuthor(tBook);
        return rows;
    }

    /**
     * 修改著作信息集
     *
     * @param tBook 著作信息集
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTBook(TBook tBook)
    {
        tBook.setUpdateTime(DateUtils.getNowDate());
        tBookMapper.deleteTBookAuthorByBOOKID(tBook.getRecordid());
        insertTBookAuthor(tBook);
        return tBookMapper.updateTBook(tBook);
    }

    /**
     * 批量删除著作信息集
     *
     * @param recordids 需要删除的著作信息集ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTBookByIds(Long[] recordids)
    {
        tBookMapper.deleteTBookAuthorByBOOKIDs(recordids);
        return tBookMapper.deleteTBookByIds(recordids);
    }

    /**
     * 删除著作信息集信息
     *
     * @param recordid 著作信息集ID
     * @return 结果
     */
    @Override
    public int deleteTBookById(Long recordid)
    {
        tBookMapper.deleteTBookAuthorByBOOKID(recordid);
        return tBookMapper.deleteTBookById(recordid);
    }

    /**
     * 新增著作作者信息集信息
     *
     * @param tBook 著作信息集对象
     */
    public void insertTBookAuthor(TBook tBook)
    {
        List<TBookAuthor> tBookAuthorList = tBook.getTBookAuthorList();
        Long recordid = tBook.getRecordid();
        if (StringUtils.isNotNull(tBookAuthorList))
        {
            List<TBookAuthor> list = new ArrayList<TBookAuthor>();
            for (TBookAuthor tBookAuthor : tBookAuthorList)
            {
                tBookAuthor.setBookid(recordid);
                list.add(tBookAuthor);
            }
            if (list.size() > 0)
            {
                tBookMapper.batchTBookAuthor(list);
            }
        }
    }
}
